/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Copyright 2002-2003 by Mark Wolgemuth, All rights reserved.             */
/* Copyright 2002-2003 by Employease, Inc.  All rights reserved.           */
/*                                                                         */
/* Redistribution and use in source and binary forms, with or without      */
/* modification, are permitted provided that the following conditions      */
/* are met:                                                                */
/*                                                                         */
/*    1. Redistributions of source code must retain the above copyright    */
/* notice, this list of conditions and the following disclaimer.           */
/*    2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in the     */
/* documentation and/or other materials provided with the distribution.    */
/*                                                                         */
/* THIS SOFTWARE IS PROVIDED BY THE MOD_ATHENA PROJECT ``AS IS'' AND ANY   */
/* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE       */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR      */
/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR        */
/* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   */
/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,     */
/* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR      */
/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT            */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   */
/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "athd.h"

/* begin private */
static athd_mail * _athd_mail_alloc(apr_pool_t *pool);
/* end private */

static athd_mail * 
_athd_mail_alloc(apr_pool_t *pool)
{
    athd_mail *msg;
    apr_pool_t *mpool;
    apr_pool_create(&mpool, pool);
    msg = (athd_mail *) apr_pcalloc(mpool, sizeof(athd_mail));
    msg->pool = mpool;
    msg->to = NULL;
    msg->replyto = NULL;
    msg->subject = NULL;
    msg->body = NULL;
    return msg;
}

athd_mail *
athd_mail_create(apr_pool_t *pool)
{
    athd_mail *mail = _athd_mail_alloc(pool);
    return mail;
}

void
athd_mail_destroy(athd_mail **msg)
{
    apr_pool_t *subpool;
    ASSERT((*msg));
    subpool = (*msg)->pool;
    apr_pool_destroy(subpool);
    (*msg) = NULL;
}

/* this localizes all memory associated with body 
 * to mail->pool 
*/
apr_status_t
athd_mail_build_for_job(athd_job *job, athd_mail *msg)
{
    char *body;
    athd_conf *conf = athd_conf_current_get();
    ASSERT(conf);
    ASSERT(job);
    ASSERT(msg);
    body = 
      apr_pstrcat(msg->pool, msg->body, "\nJob: ", job->name,
		  " Engine: ", (job->engine) ? job->engine->name : "(none)",
		  "\nTime: ", (athd_log_time(msg->pool)), 
		  "\nAthena host: ", conf->server_name,
		  NULL);
    msg->body = body;
    if (ath_list_size(job->mailaddrs) > 0)
	msg->to = ath_list_str_join(msg->pool, ",", job->mailaddrs);
    if (job->replyto)
	msg->replyto = apr_pstrdup(msg->pool, job->replyto);
    return APR_SUCCESS;
}

char * 
athd_mail_msg_to_text(athd_mail *msg)
{
    char *str_msg;
    ASSERT(msg);
    if (!msg->to)
	msg->to = ATHD_DEFAULT_MAIL_TO;
    if (!msg->replyto)
	msg->replyto = ATHD_DEFAULT_MAIL_REPLYTO;
    if (!msg->subject)
	msg->subject = ATHD_DEFAULT_MAIL_SUBJECT;
    if (!msg->body)
	msg->body = ATHD_DEFAULT_MAIL_BODY;
    str_msg = apr_pstrcat(msg->pool, 
			  "To: ", msg->to, "\n",
			  "Reply-to: ", msg->replyto, "\n",
			  "Subject: ", msg->subject, "\n\n",
			  msg->body, "\n.\n",
			  NULL);
    return str_msg;
}

apr_status_t 
athd_mail_send(athd_conf *conf, athd_mail *msg)
{
    int rv;
    apr_procattr_t *proc_mailer_attr;
    apr_proc_t *proc_mailer;
    int *exitcode;
    apr_exit_why_e *exitwhy;
    apr_file_t *pipe;
    apr_size_t msgsize;
    char *sendmsg;
    char **progargs;
    char *progname;

    ASSERT(conf);

    sendmsg = athd_mail_msg_to_text(msg);
    msgsize = sizeof(char) * strlen(sendmsg);

    apr_tokenize_to_argv(conf->os->path_sendmail, &progargs, msg->pool);
    progname = apr_pstrdup(msg->pool, progargs[0]);

    apr_procattr_create(&proc_mailer_attr, msg->pool);
    apr_procattr_cmdtype_set(proc_mailer_attr, APR_PROGRAM);
    /* setting in, out, err */
    if (conf->debug > ATHD_CORE)
	apr_procattr_io_set(proc_mailer_attr, 
			    APR_CHILD_BLOCK, APR_PARENT_BLOCK, APR_NO_PIPE);
    else
	apr_procattr_io_set(proc_mailer_attr, 
			    APR_CHILD_BLOCK, APR_NO_PIPE, APR_NO_PIPE);

    proc_mailer = (apr_proc_t *) apr_pcalloc(msg->pool, sizeof(*proc_mailer));
    rv = apr_proc_create(proc_mailer,
			 progname,
			 (const char * const *) progargs,
			 NULL,
			 proc_mailer_attr,
			 msg->pool);
    if ((!rv) && (proc_mailer->in))
	rv = apr_file_write(proc_mailer->in, sendmsg, &msgsize);
    RV_OK = apr_proc_wait(proc_mailer, exitcode, exitwhy, APR_NOWAIT);
    return rv;
}
